/**
  **************************************************************************
  * @file     flapp_net_slm.h
  * @brief    net file
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#ifndef __FLAPP_NET_SLM_H__
#define __FLAPP_NET_SLM_H__

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h> 
#include "fldrv_uart.h"
#include "flapp_system.h"
#include "flapp_clock.h"

#define FLASH_SAVE_HEAD_ADDR	0xA1B2C3D4	
#define FLASH_SET_PARA_ADDR		0x800FFC0		//ID地址
#define FLASH_MED_LOG_ADDR		0x800F800		//服药上报记录
//#define TEST_MODE

#ifdef TEST_MODE
#define TIME_PLAY_ALARM				60
#define TIME_3_HOUR						60
#define TEST_TIMESTAMP				1709135970
#define TEST_ID								11099364
#define TEST_SN								298145
#else
#define TIME_PLAY_ALARM				300
#define TIME_3_HOUR						180
#endif


#define NET_WAIT							0xFF

#define	VERSION	"V4.1.6"	
#define	HTTP_USER_NAME			"002222222222222223"
#define	HTTP_PASSWORD			"TB202102244gGEmF"
#define	HTTP_BASIC_CONTENT			"MzNmYzRmYjdkMjk3ZWZmYjhlZTdhNDQ4NjU1YzNhMjgyMTkzYmU3MmZmYTZlYWU0OTdjOTBhYzliOTk0ZDg1NzplNDZlNjY3N2Q3NWNkNTAxZjA2NmU1ZDE5YTQ5Y2FkN2YxMDc0MTViZjY4ODkyYzVhYTE4NzFhMjk4MGVkYjg0"
#define	HTTP_GET_TOKEN			"AT+HTTPPARA=\"URL\",\"https://tb12320.com/oauth/token\"\r"						//获取token
#define	HTTP_REFRESH_TOKEN		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/oauth/token\"\r"						//刷新token
#define	HTTP_GET_CUR_TIME		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/get_time?device_uid=%d\"\r"				//获取当前时间
#define	HTTP_GET_MEDICINE_TIME		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v5/device/get_setting?device_uid=%d\"\r"				//获取吃药时间
#define	HTTP_UPLOAD_MEDICINE_RECODE	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v4/device/medicine\"\r"					//上传吃药记录
#define	HTTP_UPLOAD_SIM_ICCID		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v2/device/sim\"\r"						//上传SIM卡ICCID
#define	HTTP_UPLOAD_BOX_VOLUME		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/electric_count\"\r"				//上传药盒电量
#define	HTTP_CONFIRM_VISIT		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/check_visit\"\r"					//确认随访
#define	HTTP_GET_LAST_VISIT		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/current_subsequent_visit?device_uid=%d\"\r"		//获取最近一次复诊
#define	HTTP_UPLOAD_BOX_SIGNAL		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/signal_intensity\"\r"				//上传药盒信号强度
#define	HTTP_GET_TODAY_RECODE		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/today_medicine_log?device_uid=%d\"\r"			//获取当天服药记录
#define	HTTP_GET_SOUND_VOL	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/get_volume?device_uid=%d\"\r"				//获取音量
#define	HTTP_UPLOAD_VERSION		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/version\"\r"					//上传附件版本号
#define	HTTP_GET_SOUND_LEN		"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/get_device_time_length?device_uid=%d\"\r"		//获取音长
#define	UPLOAD_MACHINE_VOLUME_HTTP	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/up_volume\"\r"				//上传药盒音量
#define	UPLOAD_MACHINE_PARAM_HTTP	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/monitoring_indicator\"\r"				//上传药盒指标参数
#define	UPLOAD_SUB_CHK_VISIT_HTTP	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/sub_check_visit\"\r"				//获取当天复诊计划是否确认了
#define	CONFIG_SEEK_HELP	"AT+HTTPPARA=\"URL\",\"https://tb12320.com/v1/device/seek_help\"\r"				//上传求助

#define AT_SET_TRB							"AT+TRB\r"			
#define AT_GET_ICCID					"AT+ICCID\r"	
#define AT_GET_SGSW						"AT+SGSW\r"	
#define AT_SET_APN						"AT+CGDCONT=1,\"IP\",\"cmiot\"\r"	
#define AT_CHECH_CPIN				"AT+CPIN?\r"	
#define AT_ACTIVATE_PDP				"AT+CGACT=1,1\r"	
#define AT_GET_CSQ						"AT+CSQ\r"	
#define AT_SET_CSCLK						"AT+CSCLK=2\r"	

#define AT_HTTP_INIT						"AT+HTTPINIT\r"
#define AT_HTTP_CID						"AT+HTTPPARA=\"CID\",\"1\"\r"
#define	AT_HTTP_CONTENT		"AT+HTTPPARA=\"CONTENT\",\"application/x-www-form-urlencoded\"\r"
#define	AT_HTTP_BASIC_AUTH		"AT+HTTPPARA=\"AUTHORIZATION\",\"Basic %s\"\r"
#define	AT_HTTP_BEARER_AUTH		"AT+HTTPPARA=\"AUTHORIZATION\",\"Bearer %s\"\r"
#define	AT_HTTP_BODY_HEAD			"AT+HTTPDATA\r"
#define	AT_HTTP_ACT_POST			"AT+HTTPACTION=1\r"
#define	AT_HTTP_ACT_GET			"AT+HTTPACTION=0\r"
#define AT_HTTP_READ_DATA			"AT+HTTPREAD=0,350\r"
#define AT_HTTP_TERM				"AT+HTTPTERM\r"

#define	AT_HTTP_TOKEN_BODY		"grant_type=password&username=%s&password=%s&scope=tuberculosis_device_read tuberculosis_device_write"
#define	AT_HTTP_REF_TOKEN_BODY	"grant_type=refresh_token&refresh_token=%s"
#define	AT_HTTP_DVC_REPORT_BODY	"device_uid=%d&iccid=%s&version_code=%s&power_flag=%d&on_flag=%d&electric_count=%d&signal_intensity=%d&api_status=%d"
#define	AT_HTTP_RPT_MED_BODY	"device_uid=%d&actualize_time=%d-%02d-%02d %02d:%02d:%02d&taken=true&interval=%d"
#define	AT_HTTP_RPT_VOLUME_BODY	"device_uid=%d&volume=%d"

#define	AT_HTTP_CHECK_VIS_BODY	"device_uid=%d"
#define	AT_HTTP_SEEK_HELP_BODY	"device_uid=%d"
#define	AT_HTTP_SUB_VISIT_BODY	"device_uid=%d"

typedef struct
{
	bool bTimeAdj;   					//是否已经校准时间
	bool bTodMedInfo;					//当天是否已经服过药，平台和本地通用，服过药了就不再查询服药了
//	bool bNeedMedAlert;  			//是否需要提醒服药,用于服药灯提醒
	bool bAheadAlert;  				//是否到了提前服药时间
	bool bTodReferralInfo;		//当天是否复诊过
	bool bNeedSubAlert;				//是否需要提醒复诊,用于复诊灯提醒
	u8 u8LastDay;	 	 					//越天检查标识，比对当前天和上一次天是否一样，不一样就是跨天
	bool network_status;
	u8  remind_flag;   //提醒标志，用于隔天刷新提醒次数
	u16  remind_cnt;   //已经提醒的次数，3次后提示未及时服药，6次后上传未服药记录
}http_control_t;

typedef enum
{
	NET_STA_CONNECTING = 0,				//正在连接
	NET_STA_CONNECTED_SUCC,		//连接成功
	NET_STA_CONNECTED_FAIL,		//连接失败
}fldrv_net_status_e;

typedef enum
{
	TIMEOUT_CLR,							//清除超时标志				
	TIMEOUT_ADD_RUN,					//超时进入等待复位
	TIMEOUT_RES_RUN,					//超时等待复位
}timeout_status_e;

typedef enum				//调整这里可以调整与平台交互接口的先后顺序
{
	API_POWER_ON		= 0,			//开机
	API_3_HOUR,								//3小时
	API_CHANGE_DAY,						//隔天
	API_KEY_POWER_OFF,				//按键关机
	API_KEY_VISIT,						//随访
	API_LOW_POW_POWER_OFF,		//低电关机
	API_STATUS_MAX,		
}fldrv_api_status_req_e;

typedef enum				//调整这里可以调整与平台交互接口的先后顺序
{
	API_GET_TOKEN		= 0,	//开机获取token
	API_RPT_DEV_PARA,		//上报设备参数
	API_GET_CURR_TIME,		//获取当前时间
	API_GET_TODAY_MED_LOG,					//获取当天服药记录
	API_GET_VOLUME,					//获取音量
	API_GET_RING_TIME,					//获取响铃时长
	API_GET_MED_TIME,					//获取服药时间
	API_GET_TODAY_SUB_LOG,					//获取当天复诊记录
	API_GET_SUB_TIME,					//获取最近复诊日期
	API_RPT_VOLUMW,					//上报音量
	API_RPT_MED_TIME,					//上传服药记录
	API_REF_TOKEN,					//刷新token
	API_CHECK_VIS,					//确认随访
	API_RPT_SEEK_HELP,					//求助
	API_HTTP_MAX,					
}fldrv_api_http_req_e;

//如果调整了以下这个enum，记得调整ParesStruct这个
typedef enum
{
	/*模组信息获取*/
	AT_STEP_TRB							= 0,	//等待模块重启
	AT_STEP_GET_ICCID				= 1,	//查询 ICCID 号码
	AT_STEP_GET_SGSW				= 2,	//查询 MEIG 软件版本信息
	AT_STEP_SET_APN					= 3,	//定义 PDP 上下文，配置APN
	AT_STEP_CHECH_CPIN				= 4,
	AT_STEP_SET_CGACT				= 5,	//PDP 上下文激活
	AT_STEP_GET_CSQ				= 6,	//获取信号质量，网络连接中
	AT_STEP_SET_CSCLK				= 7,	//设置模组工作速度，对应功耗
	/*获取token*/
	AT_TOKEN_INIT				= 8,	//等待模块重启
	AT_TOKEN_CID				,	//查询 ICCID 号码
	AT_TOKEN_URL				,	//查询 MEIG 软件版本信息
	AT_TOKEN_CONTENT			,	//定义 PDP 上下文，配置APN
	AT_TOKEN_AUTH				,	//PDP 上下文激活
	AT_TOKEN_BODY				,	//获取信号质量，网络连接中
	AT_TOKEN_BODY_WAIT			,	//获取信号质量，网络连接中
	AT_TOKEN_ACTION				,	//设置模组工作速度，对应功耗
	AT_TOKEN_READ				,	//设置模组工作速度，对应功耗
	AT_TOKEN_TERM				,	//设置模组工作速度，对应功耗
	/*上传设备信息*/
	AT_DVC_PARA_INIT			= 18,	//等待模块重启
	AT_DVC_PARA_CSQ				,	//等待模块重启
	AT_DVC_PARA_CID				,	//查询 ICCID 号码
	AT_DVC_PARA_URL				,	//查询 MEIG 软件版本信息
	AT_DVC_PARA_CONTENT			,	//定义 PDP 上下文，配置APN
	AT_DVC_PARA_AUTH			,	//PDP 上下文激活
	AT_DVC_PARA_BODY			,	//获取信号质量，网络连接中
	AT_DVC_PARA_BODY_WAIT		,	//获取信号质量，网络连接中
	AT_DVC_PARA_ACTION			,	//设置模组工作速度，对应功耗
	AT_DVC_PARA_READ			,	//设置模组工作速度，对应功耗
	AT_DVC_PARA_TERM			,	//设置模组工作速度，对应功耗
	/*获取时间*/
	AT_GET_TIME_INIT			= 29,
	AT_GET_TIME_CID				,
	AT_GET_TIME_URL				,
	AT_GET_TIME_CONTENT			,
	AT_GET_TIME_ACYION			,
	AT_GET_TIME_READ			,
	AT_GET_TIME_TERM			,
	/*获取服药计划*/
	AT_GET_MED_SET_INIT			= 36,
	AT_GET_MED_SET_CID			,
	AT_GET_MED_SET_URL			,
	AT_GET_MED_SET_CONTENT	,
	AT_GET_MED_SET_AUTH			,		
	AT_GET_MED_SET_ACYION		,
	AT_GET_MED_SET_READ			,
	AT_GET_MED_SET_TERM			,
	/*获取响铃时长*/
	AT_GET_RING_LEN_INIT			= 44,
	AT_GET_RING_LEN_CID			,
	AT_GET_RING_LEN_URL			,
	AT_GET_RING_LEN_CONTENT		,
	AT_GET_RING_LEN_AUTH			,		
	AT_GET_RING_LEN_ACYION		,
	AT_GET_RING_LEN_READ			,
	AT_GET_RING_LEN_TERM			,
	/*获取音量*/
	AT_GET_VOLUME_INIT			= 52,
	AT_GET_VOLUME_CID			,
	AT_GET_VOLUME_URL			,
	AT_GET_VOLUME_CONTENT		,
	AT_GET_VOLUME_AUTH			,		
	AT_GET_VOLUME_ACYION		,
	AT_GET_VOLUME_READ			,
	AT_GET_VOLUME_TERM			,
	/*获取复诊日期*/
	AT_GET_SUB_DATE_INIT			= 60,
	AT_GET_SUB_DATE_CID			,
	AT_GET_SUB_DATE_URL			,
	AT_GET_SUB_DATE_CONTENT		,
	AT_GET_SUB_DATE_AUTH			,		
	AT_GET_SUB_DATE_ACYION		,
	AT_GET_SUB_DATE_READ			,
	AT_GET_SUB_DATE_TERM			,
	/*获取当天服药记录*/
	AT_GET_TOD_MED_INIT			= 68,
	AT_GET_TOD_MED_CID			,
	AT_GET_TOD_MED_URL			,
	AT_GET_TOD_MED_CONTENT		,
	AT_GET_TOD_MED_AUTH			,		
	AT_GET_TOD_MED_ACYION		,
	AT_GET_TOD_MED_READ			,
	AT_GET_TOD_MED_TERM			,
	/*获取当天复诊记录*/
	AT_GET_SUB_CHECK_INIT			= 76,
	AT_GET_SUB_CHECK_CID			,
	AT_GET_SUB_CHECK_URL			,
	AT_GET_SUB_CHECK_CONTENT	,
	AT_GET_SUB_CHECK_AUTH			,	
	AT_GET_SUB_BODY						,
	AT_GET_MED_BODY_WAIT			,
	AT_GET_SUB_CHECK_ACYION		,
	AT_GET_SUB_CHECK_READ			,
	AT_GET_SUB_CHECK_TERM			,
	/*上传服用记录*/
	AT_RPT_MED_INIT			= 86,
	AT_RPT_MED_CID			,
	AT_RPT_MED_URL			,
	AT_RPT_MED_CONTENT		,
	AT_RPT_MED_AUTH			,	
	AT_RPT_MED_BODY			,	//获取信号质量，网络连接中
	AT_RPT_MED_BODY_WAIT	,	//获取信号质量，网络连接中
	AT_RPT_MED_ACYION		,
	AT_RPT_MED_READ			,
	AT_RPT_MED_TERM			,
	/*上传音量设置*/
	AT_RPT_VOLUME_INIT			= 96,
	AT_RPT_VOLUME_CID			,
	AT_RPT_VOLUME_URL			,
	AT_RPT_VOLUME_CONTENT		,
	AT_RPT_VOLUME_AUTH			,	
	AT_RPT_VOLUME_BODY			,	//获取信号质量，网络连接中
	AT_RPT_VOLUME_BODY_WAIT		,	//获取信号质量，网络连接中
	AT_RPT_VOLUME_ACYION		,
	AT_RPT_VOLUME_READ			,
	AT_RPT_VOLUME_TERM			,
	/*上传随访记录*/
	AT_RPT_CHECK_VIS_INIT			= 106,
	AT_RPT_CHECK_VIS_CID			,
	AT_RPT_CHECK_VIS_URL			,
	AT_RPT_CHECK_VIS_CONTENT		,
	AT_RPT_CHECK_VIS_AUTH			,	
	AT_RPT_CHECK_VIS_BODY			,	//获取信号质量，网络连接中
	AT_RPT_CHECK_VIS_BODY_WAIT		,	//获取信号质量，网络连接中
	AT_RPT_CHECK_VIS_ACYION			,
	AT_RPT_CHECK_VIS_READ			,
	AT_RPT_CHECK_VIS_TERM			,
	/*上传求助*/
	AT_RPT_SEEK_HELP_INIT			= 116,
	AT_RPT_SEEK_HELP_CID			,
	AT_RPT_SEEK_HELP_URL			,
	AT_RPT_SEEK_HELP_CONTENT		,
	AT_RPT_SEEK_HELP_AUTH			,	
	AT_RPT_SEEK_HELP_BODY			,	//获取信号质量，网络连接中
	AT_RPT_SEEK_HELP_BODY_WAIT		,	//获取信号质量，网络连接中
	AT_RPT_SEEK_HELP_ACYION			,
	AT_RPT_SEEK_HELP_READ			,
	AT_RPT_SEEK_HELP_TERM			,
	/*刷新token*/
	AT_REF_TOKEN_INIT				= 126,	//等待模块重启
	AT_REF_TOKEN_CID				,	//查询 ICCID 号码
	AT_REF_TOKEN_URL				,	//查询 MEIG 软件版本信息
	AT_REF_TOKEN_CONTENT			,	//定义 PDP 上下文，配置APN
	AT_REF_TOKEN_AUTH				,	//PDP 上下文激活
	AT_REF_TOKEN_BODY				,	//获取信号质量，网络连接中
	AT_REF_TOKEN_BODY_WAIT			,	//获取信号质量，网络连接中
	AT_REF_TOKEN_ACTION				,	//设置模组工作速度，对应功耗
	AT_REF_TOKEN_READ				,	//设置模组工作速度，对应功耗
	AT_REF_TOKEN_TERM				,	//设置模组工作速度，对应功耗
	
	AT_STEP_TEST_CSQ				,
	AT_STEP_TEST_ICCID			,
	AT_RUNNINGTYPE_ENUM_END
}fldrv_at_running_type_e;

u32 flapp_get_dec_num_from_str(char *str, char stopChar);	//将字符串中一段数字转换成数字输出
void flapp_get_sub_para_ctrl(sub_para_t* para);
void flapp_power_on_net_init(void);
void flapp_set_dvc_ver_and_type(u8 ver,u8 type);
void flapp_set_report_data(u16 len,u8 *buf);
void flapp_net_send_slm_run(void);
void flapp_net_rcv_slm_run(void);
void flapp_set_http_req(fldrv_api_http_req_e var,fldrv_api_status_req_e api);
u8 flapp_get_csq_status(void);
u32 flapp_get_id_status(void);
u32 flapp_get_http_status(void);
u8 flapp_get_net_status(void);
u8 flapp_get_net_status(void);
void flapp_insert_inquire_cmd(u8 step);
void flapp_set_cur_med_time(void);
void flapp_offline_save_med_log(void);
void flapp_reset_tod_med_info(void);
#endif
